/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package qs.crm.modules.serve.sct_service.service.impl;

import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import qs.crm.modules.serve.sct_service.domain.SctService;
import qs.crm.modules.serve.sct_service.repository.SctServiceRepository;
import qs.crm.modules.serve.sct_service.service.SctServiceService;
import qs.crm.modules.serve.sct_service.service.dto.SctServiceDto;
import qs.crm.modules.serve.sct_service.service.dto.SctServiceQueryCriteria;
import qs.crm.modules.serve.sct_service.service.mapstruct.SctServiceMapper;
import qs.crm.utils.FileUtil;
import qs.crm.utils.PageUtil;
import qs.crm.utils.QueryHelp;
import qs.crm.utils.ValidationUtil;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author tong
* @date 2023-06-03
**/
@Service
@RequiredArgsConstructor
public class SctServiceServiceImpl implements SctServiceService {

    private final SctServiceRepository sctServiceRepository;
    private final SctServiceMapper sctServiceMapper;

    @Override
    public Map<String,Object> queryAll(SctServiceQueryCriteria criteria, Pageable pageable){
        Page<SctService> page = sctServiceRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(sctServiceMapper::toDto));
    }

    @Override
    public List<SctServiceDto> queryAll(SctServiceQueryCriteria criteria){
        return sctServiceMapper.toDto(sctServiceRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public SctServiceDto findById(Long svrId) {
        SctService sctService = sctServiceRepository.findById(svrId).orElseGet(SctService::new);
        ValidationUtil.isNull(sctService.getSvrId(),"SctService","svrId",svrId);
        return sctServiceMapper.toDto(sctService);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SctServiceDto create(SctService resources) {
        return sctServiceMapper.toDto(sctServiceRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(SctService resources) {
        SctService sctService = sctServiceRepository.findById(resources.getSvrId()).orElseGet(SctService::new);
        ValidationUtil.isNull( sctService.getSvrId(),"SctService","id",resources.getSvrId());
        sctService.copy(resources);
        sctServiceRepository.save(sctService);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long svrId : ids) {
            sctServiceRepository.deleteById(svrId);
        }
    }

    @Override
    public void download(List<SctServiceDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (SctServiceDto sctService : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("服务类型", sctService.getSvrType());
            map.put("概要", sctService.getSvrTitle());
            map.put("客户编号", sctService.getSvrCustNo());
            map.put("客户名称", sctService.getSvrCustName());
            map.put("服务状态", sctService.getSvrStatus());
            map.put("服务请求", sctService.getSvrRequest());
            map.put("创建人编号", sctService.getSvrCreateId());
            map.put("创建人名称", sctService.getSvrCreateBy());
            map.put("创建时间", sctService.getSvrCreateDate());
            map.put("分配给的员工编号", sctService.getSvrDueId());
            map.put("分配给的员工姓名	", sctService.getSvrDueTo());
            map.put("分配时间", sctService.getSvrDueDate());
            map.put("服务处理	", sctService.getSvrDeal());
            map.put("处理人编号", sctService.getSvrDealId());
            map.put("处理人姓名	", sctService.getSvrDealBy());
            map.put("处理日期", sctService.getSvrDealDate());
            map.put("结果	", sctService.getSvrResult());
            map.put("满意度", sctService.getSvrSatisfy());
            map.put("逻辑删除", sctService.getIsDelete());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}